#include <CommonHelper.h>
#include <QDebug>

CommonHelper* CommonHelper::Instance=0;

CommonHelper* CommonHelper::commonInstance()
{
    if(Instance==0)
        Instance =new CommonHelper();
    return Instance;
}

CommonHelper::CommonHelper()
{
    logClient=loghelper::getInstance();
}

QString CommonHelper::GetGuid(){
    try{
        QUuid id = QUuid::createUuid();
        QString strGuid =id.toString(QUuid::Id128);
        return  strGuid;
    }
    catch(std::exception &e){
        QString err=e.what();
        logClient->WriteLog("Exception","获取GUID异常:"+err);
    }
    catch(...)
    {
        logClient->WriteLog("Exception","获取GUID捕获到未知异常;");
    }

}

QString CommonHelper::GetDateTimeStr(){
    try{
        QDateTime current=QDateTime::currentDateTime();
        QString dateStr=current.toString("yyyy-MM-dd hh:mm:ss");
        return dateStr;
    }
    catch(std::exception &e){
        QString err=e.what();
        logClient->WriteLog("Exception","获取时间字符串异常:"+err);
    }
    catch(...)
    {
        logClient->WriteLog("Exception","获取时间字符串捕获到未知异常;");
    }
}

QString CommonHelper::TenToSixteen(int num, int bit){
    QString hex = QString::number(num, 16);
    if (hex.length() < bit) {
        int n = bit - hex.length();
        for (int i = 0; i < n; i++) {
            hex.prepend("0");
        }
    }

    QString result;
    for (int i = 0; i < hex.length(); ++i) {
        result.append(hex.at(i));
        if (i % 2 == 1 && i != hex.length() - 1) {
            result.append(" ");
        }
    }
    return result;
}

QByteArray CommonHelper::GetSendData(QString sendData){
    sendData= sendData.trimmed();
    QByteArray result;
    for(int i = 0;i<sendData.size();i++)
    {
        if(sendData[i]==' ')
            continue;   //跳过
        int num  = sendData.mid(i,2).toUInt(nullptr,16);
        i++;
        result.append(num);
    }
    return result;
}

QString CommonHelper::GetCrcCheck(QString data) {
    QByteArray dataArray = hexStringToByteArray(data);

    int len=dataArray.size();
    uint16_t wcrc=0XFFFF;//预置16位crc寄存器，初值全部为1
    uint8_t temp;//定义中间变量
    int i=0,j=0;//定义计数
    for(i=0;i<len;i++)//循环计算每个数据
    {
        temp=dataArray.at(i);
        wcrc^=temp;
        for(j=0;j<8;j++){
            //判断右移出的是不是1，如果是1则与多项式进行异或。
            if(wcrc&0X0001){
                wcrc>>=1;//先将数据右移一位
                wcrc^=0XA001;//与上面的多项式进行异或
            }
            else//如果不是1，则直接移出
                wcrc>>=1;//直接移出
        }
    }
    return TenToSixteen(uint8_t(wcrc>>8),1)+" "+TenToSixteen(uint8_t(wcrc),1);
}

QString CommonHelper::SixteenToTen(QString data){
    data.replace(" ","");
    bool ok;
    int dec = data.toInt(&ok,16);
    QString num = QString("%1").arg(dec,0,10);
    return  num;
}

QString CommonHelper::SixteenToTwo(QString data){
    QString bin = QString::number(data.toInt(NULL, 16), 2);
    return bin;
}

QByteArray CommonHelper::hexStringToByteArray(QString HexString)
{
    bool ok;
    QByteArray ret;
    HexString = HexString.trimmed();
    HexString = HexString.simplified();
    QStringList sl = HexString.split(" ");

    foreach (QString s, sl)
    {
        if(!s.isEmpty())
        {
            char c = static_cast<char>(s.toInt(&ok, 16) & 0xFF);
            if(ok)
            {
                ret.append(c);
            }
            else
            {

            }
        }
    }

    return ret;
}

void CommonHelper::Sleep(unsigned int msec)
{
    QTime reachTim=QTime::currentTime().addMSecs(msec);
    while (QTime::currentTime()<reachTim) {
        QCoreApplication::processEvents(QEventLoop::AllEvents,100);
    }
}
